package com.woniuxy.trade.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.common.model.dto.trade.BorrowRecordDTO;
import com.woniuxy.common.model.vo.trade.BorrowRecordVo;
import com.woniuxy.common.result.Result;
import com.woniuxy.trade.entity.BorrowRecord;
import com.woniuxy.trade.service.IBorrowRecordService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2023 -07-31
 * @desc
 */
@RestController
public class TradeController {

    @Autowired
    private IBorrowRecordService borrowRecordService;

    @ApiOperation("借款申请...")
    @PostMapping("/auth/trade/borrow")
    public String borrowRecord(@RequestBody BorrowRecordDTO borrowRecordDTO) {

        //进行 借款
        borrowRecordService.borrow(borrowRecordDTO);
        return "借款成功...";

    }

    @ApiOperation("查询借款记录")
    @GetMapping("/trade/search")
    public Result searchborrowRecord(String name, String createDate) {

        LambdaQueryWrapper<BorrowRecord> borrowRecordLambdaQueryWrapper = Wrappers.lambdaQuery(BorrowRecord.class);

//        if (StringUtils.isNotBlank(name)) {
//            borrowRecordLambdaQueryWrapper.like(BorrowRecord::getName, name);
//        }
//        if (StringUtils.isNotBlank(createDate)) {
//            borrowRecordLambdaQueryWrapper.gt(BorrowRecord::getCreateDate, createDate);
//        }
        borrowRecordLambdaQueryWrapper.like(
                StringUtils.isNotBlank(name),  //条件为true 就执行后面的条件
                BorrowRecord::getName, name
        );
        borrowRecordLambdaQueryWrapper.gt(
                StringUtils.isNotBlank(createDate),
                BorrowRecord::getCreateDate, createDate
        );

        //BorrowRecord ==> BorrowRecordVo     List<BorrowRecordVo>
        List<BorrowRecord> list = borrowRecordService.list(borrowRecordLambdaQueryWrapper);
//        ArrayList<BorrowRecordVo> vos = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            BorrowRecord borrowRecord = list.get(i);
//            BorrowRecordVo borrowRecordVo = new BorrowRecordVo();
//            borrowRecordVo.setBorrowRecordNo(borrowRecord.getBorrowRecordNo());
//            borrowRecordVo.setPeriod(borrowRecord.getPeriod());
//            vos.add(borrowRecordVo);
//        }

        //工具类 或者 AI 给你优化
        List<BorrowRecordVo> borrowRecordVos = BeanUtil.copyToList(list, BorrowRecordVo.class);

        return Result.ok(borrowRecordVos);
    }

    @ApiOperation("借款记录审核")
    @GetMapping("/auth/trade/status/{borrowId}/{status}")
    public String tradeChangeStatus(
            @PathVariable("borrowId") Long borrowId,
            @PathVariable("status") Integer status) {
        String result = borrowRecordService.changeStatus(borrowId, status);
        return result;
    }
//
//    @ApiOperation("机构认购投资理财产品")
//    @PostMapping("/auth/trade/agency/bid/add/{borrowId}/{amount}")
//    public Result addAgencyBid(
//            @PathVariable("borrowId") Long borrowId,       //借款的id
//            @PathVariable("amount") Integer amount) {      //投机的金额
//
//        //投资认证流程
//        Map<String, Object> map = borrowRecordService.addAgencyBid(borrowId, amount);
//        return  Result.ok(map);
//    }
//
//    @ApiOperation("机构认购投资的审核")
//    @PostMapping("/trade/agency/bid/status")
//    public String agencyBidStatus(
//            @RequestParam("borrowId")Long borrowId,         //借款id
//            @RequestParam("commission")String commission,   //平台佣金
//            @RequestParam("status")String status            //审核状态
//    ){
//
//       String result =  borrowRecordService.agencyBidStatus(borrowId,commission,status);
//       return result;
//
//    }
}
